////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.data {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.events.EventDispatcher;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="connected", type="plantool.data.DatabaseController")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class DatabaseController extends EventDispatcher implements IDatabaseController {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		public static const CONNECTED:String		= "connected";
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _connected:Boolean		= false;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get connected():Boolean {
			return _connected;
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function DatabaseController() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		protected function onConnected():void {
			//set connected
			_connected = true;
			
			//dispatch connected event
			dispatchEvent(new Event(CONNECTED));
		}
		
		public function close():void {
			throw new Error("plantool.system.data.DatabaseController::close - abstract method, should be overridden in subclass");
		}
		
		public function connect():void {
			throw new Error("plantool.system.data.DatabaseController::connect - abstract method, should be overridden in subclass");
		}
		
		public function createDateFromTimestamp(timestamp:Number):Date {
			throw new Error("plantool.system.data.DatabaseController::createDateFromTimestamp - abstract method, should be overridden in subclass");
		}
		
		public function createTable(table:Table, onCompleteHandler:Function):void {
			throw new Error("plantool.system.data.DatabaseController::createTable - abstract method, should be overridden in subclass");
		}
		
		public function formatDateField(field:String):String {
			return field;
		}
		
		public function formatDate(date:Date):String {
			return date.valueOf().toString();
		}
		
		public function getFieldName(type:uint):String  {
			throw new Error("!!UNKNOWN FIELD TYPE!! - Use FieldDefenitions class");
			return "";
		}
		
		public function query(q:String, onCompleteHandler:Function, onErrorHandler:Function, itemClass:Class = null, userdata:* = null):void {
			throw new Error("plantool.system.data.DatabaseController::query - abstract method, should be overridden in subclass");
		}
		
		public function transaction(querys:Array, onCompleteHandler:Function, onErrorHandler:Function, userdata:* = null):void {
			throw new Error("plantool.system.data.DatabaseController::transaction - abstract method, should be overridden in subclass");
		}
	}
}